package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ImageView;
import com.example.myapplication.model.Review;
import java.util.List;

public class ReviewAdapter extends BaseAdapter {
    private Context context;
    private List<Review> reviewList;

    public ReviewAdapter(Context context, List<Review> reviewList) {
        this.context = context;
        this.reviewList = reviewList;
    }

    @Override
    public int getCount() {
        return reviewList.size();
    }

    @Override
    public Object getItem(int position) {
        return reviewList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_review, parent, false);
            holder = new ViewHolder();
            holder.textViewReviewer = convertView.findViewById(R.id.textViewReviewer);
            holder.textViewReviewComment = convertView.findViewById(R.id.textViewReviewComment);
            holder.textViewReviewRating = convertView.findViewById(R.id.textViewReviewRating);
            holder.textViewLikeCount = convertView.findViewById(R.id.textViewLikeCount);
            holder.imageViewLike = convertView.findViewById(R.id.imageViewLike);
            holder.textViewReviewTime = convertView.findViewById(R.id.textViewReviewTime);
            holder.textViewExpandCollapse = convertView.findViewById(R.id.textViewExpandCollapse);
            holder.isExpanded = false;
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Review review = reviewList.get(position);
        holder.textViewReviewer.setText(review.getReviewer());
        holder.textViewReviewComment.setText(review.getComment());
        holder.textViewReviewRating.setText("评分：" + review.getRating());
        holder.textViewLikeCount.setText(String.valueOf(review.getLikeCount()));
        holder.textViewReviewTime.setText(review.getTime());

        // 处理评论展开/收起
        final String comment = review.getComment();
        if (comment != null && comment.length() > 50) {
            holder.textViewExpandCollapse.setVisibility(View.VISIBLE);
            updateCommentVisibility(holder, comment);

            holder.textViewExpandCollapse.setOnClickListener(v -> {
                holder.isExpanded = !holder.isExpanded;
                updateCommentVisibility(holder, comment);
            });

            holder.textViewReviewComment.setOnClickListener(v -> {
                holder.isExpanded = !holder.isExpanded;
                updateCommentVisibility(holder, comment);
            });
        } else {
            holder.textViewExpandCollapse.setVisibility(View.GONE);
            holder.textViewReviewComment.setMaxLines(Integer.MAX_VALUE);
        }
        holder.imageViewLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点赞持久化
                com.example.myapplication.db.DBManager dbManager = new com.example.myapplication.db.DBManager(context);
                int newLikeCount = dbManager.addLikeToReview(review.getReviewer(), review.getComment(),
                        review.getTime());
                if (newLikeCount >= 0) {
                    review.setLikeCount(newLikeCount);
                    holder.textViewLikeCount.setText(String.valueOf(newLikeCount));
                }
            }
        });
        return convertView;
    }

    static class ViewHolder {
        TextView textViewReviewer;
        TextView textViewReviewComment;
        TextView textViewReviewRating;
        TextView textViewLikeCount;
        TextView textViewReviewTime;
        TextView textViewExpandCollapse;
        ImageView imageViewLike;
        boolean isExpanded;
    }

    private void updateCommentVisibility(ViewHolder holder, String comment) {
        if (holder.isExpanded) {
            holder.textViewReviewComment.setMaxLines(Integer.MAX_VALUE);
            holder.textViewExpandCollapse.setText("收起");
        } else {
            holder.textViewReviewComment.setMaxLines(2);
            holder.textViewExpandCollapse.setText("展开");
        }
        holder.textViewReviewComment.requestLayout();
    }
}
